/**
 * 
 */
package com.alianzamedica.controllers.prescription;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Doctor;
import com.alianzamedica.businessobject.Prescription;
import com.alianzamedica.businessobject.PrescriptionDetail;
import com.alianzamedica.tools.Enviroment;

/**
 * @author RMarquez_Inf
 * 
 */
public class ViewPrescriptionAction extends Action {
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.
	 * ActionMapping, org.apache.struts.action.ActionForm,
	 * javax.servlet.ServletRequest, javax.servlet.ServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		String id = request.getParameter("id");
		int idN = Integer.parseInt(id);
		Prescription prescription = new Prescription();
		if (id != null) {
			
			prescription.setId(idN);
			Iterator<Prescription> iterator = search
					.searchObjects(prescription).iterator();
			Prescription doctor = null;
			if (iterator.hasNext()) {
				prescription = (Prescription) iterator.next();
			}
		}
		
		ArrayList<PrescriptionDetail> details = prescription.getDetails();
		for(PrescriptionDetail detail:details){
			prescription.detail.add(detail);
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("prescription", prescription);
		return mapping.findForward("success");
	}
}
